﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;
using BitcoinExchange.Data;


namespace BitcoinExchange.Contract
{
    public class KorbitContract
    {
        MainInfo _info;
        public DataOpenOrder OpenOrderTable;

        public KorbitContract(MainInfo info)
        {
            _info = info;
            OpenOrderTable = new DataOpenOrder();
        }

        public void RetrivedOpenOrder()
        {
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (!_info.IsLogin())
                return;

            Dictionary<string, string> dicURLParameter = _info.GetKorbitURLParameterAccessKey();

            //접속 주소를 입력한다.
            strURL = _info.GetKorbitURL() + StringURL.URL_KORBIT_ORDER_OPEN;
            strParameters = _info.ConvertURLParameter(dicURLParameter);
            strURL = strURL + "?" + strParameters;

            strData = _info.RequestData(strURL, "");

            if (strData.Length == 0)
                return;

            ConvertToOpenOrder(strData);
        }

        public List<string> OrderIDList()
        {
            return OpenOrderTable.OrderIDList();
        }

        public Dictionary<string, string> OrderInfo(string strOrderID)
        {
            return OpenOrderTable.OrderInfo(strOrderID);
        }

        private void ConvertToOpenOrder(string strData)
        {
            DataOpenOrder objTable = new DataOpenOrder();

            string strOrderID;
            string strOrderType;
            double dbBTCPrice;
            double dbBTCAmount;
            double dbBTCOpenAmount;

            try
            {
                objTable.Clear();

                JArray oItems = JArray.Parse(strData);

                foreach (JToken oSubItem in oItems)
                {
                    try{

                        strOrderID = oSubItem["id"].ToString();
                        strOrderType = oSubItem["type"].ToString();

                        if (strOrderType == "ask")
                            strOrderType = "sell";
                        else
                            strOrderType = "buy";

                        JToken oSubSubItem = oSubItem["price"];
                        dbBTCPrice = Double.Parse(oSubSubItem["value"].ToString());
                        oSubSubItem = oSubItem["total"];
                        dbBTCAmount = Double.Parse(oSubSubItem["value"].ToString());
                        oSubSubItem = oSubItem["open"];
                        dbBTCOpenAmount = Double.Parse(oSubSubItem["value"].ToString());


                        objTable.AddRow(strOrderID, strOrderType, dbBTCPrice, dbBTCAmount, dbBTCOpenAmount);
                    }catch(Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            if (objTable.RowCount() == 0)
                return;

            OpenOrderTable = objTable;
        }

    }
}
